<?php
class GroupBase extends Zend_Db_Table
{
	protected $_name = "groupbase";
	
	public function getAllGroups()
	{
		$select = $this->_db->select()
							->from(array('groupBase'=>$this->_name),array('id','groupname'));
		
		$results = $this->getAdapter()->fetchAll($select);
		return $results;
	}
	
	public function getUserGroup($user)
	{
		$select = $this->_db->select()
							->from(array('groupBase'=>'groupbase'),null)
							->join(array('membershipBase'=>'membershipbase'),
											'groupBase.groupname = membershipBase.group_name')
							->join(array('projectRoleActor'=>'projectroleactor'),
											'projectRoleActor.roletypeparameter = membershipBase.group_name')
							->join(array('projectRole'=>'projectrole'),
											'projectRole.id = projectRoleActor.projectroleid')
							->where("user_name = ?",$user)
							->order("projectRole.id DESC");
		$results = $this->getAdapter()->fetchAll($select);
		return $results;
											
	}
	
	public function getGroupMembersCount($groupId)
	{
		$select = $this->_db->select()
							->from(array('groupBase'=>$this->_name),null)
							->join(array('membershipBase'=>'membershipbase'),
												'groupBase.groupname = membershipBase.group_name',null)
							->join(array('userBase'=>'userbase'),
												'userBase.name = membershipBase.user_name',"count(*) as c")
							->where('groupBase.id = ?', $groupId);
		$result = $this->getAdapter()->fetchOne($select);
		return $result;
	}
}